package sjtu.pirate.format;

import org.joda.time.DateTime;
import org.joda.time.Duration;
/**
 * @author wilbur.ma@qq.com
 * @date 2011.05.26
 */
public class FormResult {
	public final static Duration stdDuration = new Duration(30000);  // 30s
	private Form form;
	private boolean isLogin;
	private int returnCode;
	private DateTime startTime;
	private DateTime endTime;
	private ResultType type;
	
	public enum ResultType {
		UNSET("Form result not set."),
		OK("OK"),
		SERVER_ERR("Server error, return code 4xx/5xx."),
		LOGIN("Logined with wrong name or password."),
		TIMEOUT("Timeout.");
		private String info;
		private ResultType(String s) {
			this.info = s;
		}
		public String toString() {
			return info;
		}
	}
	
	public FormResult() {
		form = null;
		isLogin = false;
		returnCode = -1;
		startTime = endTime = null;
		type = ResultType.UNSET;
	}
	
	public FormResult(Form f, boolean isLogin, int returnCode, DateTime start, DateTime end) {
		this.form = f;
		this.isLogin = isLogin;
		this.returnCode = returnCode;
		this.startTime = start;
		this.endTime = end;
		setResultType();
	}
	
	public void setResultType() {
		int code = returnCode / 100;
		if (code == 4 || code == 5)
			type = ResultType.SERVER_ERR;
		else if (isLogin)
			type = ResultType.LOGIN;
		else if (new Duration(startTime, endTime).isLongerThan(stdDuration))
			type = ResultType.TIMEOUT;
		else
			type = ResultType.OK;
	}
	
	public ResultType getResultType() {
		return type;
	}
	
	public Form getForm() {
		return form;
	}

	public void setForm(Form form) {
		this.form = form;
	}

	public boolean isLogin() {
		return isLogin;
	}

	public void setLogin(boolean isLogin) {
		this.isLogin = isLogin;
	}

	public int getReturnCode() {
		return returnCode;
	}

	public void setReturnCode(int returnCode) {
		this.returnCode = returnCode;
	}

	public DateTime getStartTime() {
		return startTime;
	}

	public void setStartTime(DateTime startTime) {
		this.startTime = startTime;
	}

	public DateTime getEndTime() {
		return endTime;
	}

	public void setEndTime(DateTime endTime) {
		this.endTime = endTime;
	}
}
